package com.chatgpt.platformbasedchatgpt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chatgpt.platformbasedchatgpt.dao.History;
import com.chatgpt.platformbasedchatgpt.mapper.HistoryMapper;
import com.chatgpt.platformbasedchatgpt.service.HistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Chat
 * @since 2023-12-03
 */
@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    HistoryMapper historyMapper;

    @Override
    public List<History> getHistory(int userId){
        List<History> historyList = historyMapper.selectList(new LambdaQueryWrapper<History>().eq(History::getUserId, userId));
        return historyList;
    }

    @Override
    public int saveHistory(History history){
        History one = historyMapper.selectOne(new LambdaQueryWrapper<History>()
                .eq(History::getUserId, history.getUserId())
                .eq(History::getCnt, history.getCnt())
        );
        history.setUpdateTime(new Date());
        if(one!=null){
            history.setId(one.getId());
           return historyMapper.updateById(history);
        }else {
            return   historyMapper.insert(history);
        }

    }

}
